#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>		/* signal */
#include "a311d-setenv.h"

/**
  ******************************************************************************
  * @file           : a311d-setenv.c
  * @brief          : implement for a311d-setenv.c file.
  *                   This file contains the common implement of the application.
  * @author			: ac
  * @attention
  *
  *		Copyright (c) 2021 Technology Co., Ltd.
  *
  *******************************************************************************
  */


/*
 * globla variable
 */

static uint16_t series = 0;
static uint16_t model = 0;


/*
 * function declaration
 */
int create_partition_file(void);


/**
 * create_partition_file - create partition temp file.
 * @none
 */
int create_partition_file(void)
{
	char buff[128] = {0};
	int ret = 0xac;

	sprintf(buff, "dd if=%s of=%s bs=1k count=64 > /dev/null", 
					PARTITION_NUM, 
					DEVINFO);
	ret = system(buff);

	return ret == 0 ? 0 : -1;
}

/**
 * open_partition_file - open partition temp file.
 * @file_name - file name
 */
FILE * open_partition_file(char * file_name)
{
	FILE * fp = NULL;

	fp = fopen(file_name, "r");
	if (fp == NULL) {
		printf("fopen [%s] file fail...\n", file_name);
	}

	return fp;
}

/**
 * close_partition_file - close partition temp file.
 *  @fp	: file handler pointer
 */
void close_partition_file( FILE * fp)
{
	if (fp != NULL) {
		fclose(fp);
	}
}


int hdls_get_sn(uint8_t* sn)
{
    FILE *fp;
    uint32_t ret = 0;
    uint8_t tmpbuf[1024] = {0};
    uint8_t tmp_sn[13] = {0};

    //从分区读取到文件
    if(dd_flag == 0)
    {
        dd_flag = 1;
        char tmpbuf[200] = {0};
        sprintf(tmpbuf, "dd if=%s of=%s bs=1k count=64 > /dev/null", plat_config.device_info, DEVINFO);
        //system("dd if=/dev/mmcblk2p3 of=/tmp/xag-dls/hdls_devinfo bs=1k count=64 > /dev/null");
        system(tmpbuf);
    }

    fp = fopen(DEVINFO, "r");
    if(fp == NULL)
    {
        LOG(ERROR) << "fopen file hdls_devinfo fail！！！";
        return -1;
    }

    ret = fread(tmpbuf, 1, sizeof(tmpbuf), fp);
    if(ret <= 0)
    {
        LOG(ERROR) << "fread file hdls_devinfo fail！！！";
        fclose(fp);
        return -1;
    }
    fclose(fp);

    memcpy(tmp_sn, tmpbuf+0x88, 12);

    if(strlen((char *)tmp_sn) != 12)
    {
        LOG(ERROR) << "sn error！！！";
        return -1;
    }

    memcpy(sn, tmp_sn, 12);
    //LOG(INFO) << "get sn success, sn:" << sn;
    return 0;
}

//获取设备型号
int hdls_get_devtype()
{
    FILE *fp;
    int ret = 0;
    int devtype = 0;
    
    char tmpbuf[1024] = {0};

    //从分区读取到文件
    if(dd_flag == 0)
    {
        dd_flag = 1;
        char tmpbuf[200] = {0};
        sprintf(tmpbuf, "dd if=%s of=%s bs=1k count=64 > /dev/null", plat_config.device_info, DEVINFO);
        //system("dd if=/dev/mmcblk2p3 of=/tmp/xag-dls/hdls_devinfo bs=1k count=64 > /dev/null");
        system(tmpbuf);
    }

    fp = fopen(DEVINFO, "r");
    if(fp == NULL)
    {
        LOG(ERROR) << "fopen hdls_devinfo fail！！！";
        devtype = HDLS_DEVICE_TYPE_APC;
        return devtype;
    }

    ret = fread(tmpbuf, 1, sizeof(tmpbuf), fp);
    if(ret <= 0)
    {
        LOG(ERROR) << "fread hdls_devinfo fail！！！";
        fclose(fp);
        devtype = HDLS_DEVICE_TYPE_APC;
        return devtype;
    }
    fclose(fp);

    memcpy(&series, tmpbuf+0x5a,2);
    memcpy(&model, tmpbuf+0x58,2);

    switch (series)
    {
    case SERIES_P:
        devtype = HDLS_DEVICE_TYPE_HDLS3;
        system("echo 106 > /etc/hdls_devtype");
        break;
    case SERIES_M:
        devtype = HDLS_DEVICE_TYPE_M;
        system("echo 108 > /etc/hdls_devtype");
        break;
    case SERIES_A:
        devtype = HDLS_DEVICE_TYPE_APC;
        system("echo 105 > /etc/hdls_devtype");
        break;
    case SERIES_V:
        devtype = HDLS_DEVICE_TYPE_HDLS3;
        system("echo 106 > /etc/hdls_devtype");
        break;
    case SERIES_S:
        devtype = HDLS_DEVICE_TYPE_APC;
        system("echo 105 > /etc/hdls_devtype");
        break;
    case SERIES_M_F:
        devtype = HDLS_DEVICE_TYPE_F;
        system("echo 109 > /etc/hdls_devtype");
        break;
    default:
        LOG(ERROR) << "get dev type fail, series not find！！！,series=" << series;
        devtype = HDLS_DEVICE_TYPE_APC;
        system("echo 105 > /etc/hdls_devtype");
        return devtype;
    }

    LOG(INFO) << "get dev type success, dev type:" << (uint16_t)devtype;
    LOG(INFO) << "get dev type success, dev series:" << (uint16_t)series;
    return devtype;
}

int read_dev_series(uint16_t* seri, uint16_t* mod)
{
    if(seri==NULL || mod==NULL)
        return -1;
    
    FILE *fp;
    int ret = 0;
    
    char tmpbuf[1024] = {0};

    //从分区读取到文件
    if(dd_flag == 0)
    {
        dd_flag = 1;
        char tmpbuf[200] = {0};
        sprintf(tmpbuf, "dd if=%s of=%s bs=1k count=64 > /dev/null", plat_config.device_info, DEVINFO);
        //system("dd if=/dev/mmcblk2p3 of=/tmp/xag-dls/hdls_devinfo bs=1k count=64 > /dev/null");
        system(tmpbuf);
    }

    fp = fopen(DEVINFO, "r");
    if(fp == NULL)
    {
        LOG(ERROR) << "fopen hdls_devinfo fail！！！";
        return -1;
    }

    ret = fread(tmpbuf, 1, sizeof(tmpbuf), fp);
    if(ret <= 0)
    {
        LOG(ERROR) << "fread hdls_devinfo fail！！！";
        fclose(fp);
        return -1;
    }
    fclose(fp);

    memcpy(&series, tmpbuf+0x5a,2);
    memcpy(&model, tmpbuf+0x58,2);

    *seri = series;
    *mod = model;
    LOG(INFO) << "series:" << (uint16_t)*seri << ",model:" << (uint16_t)*mod;

    return 0;
}

//获取地区码
uint8_t hdls_get_area()
{
    uint8_t area;
    FILE *fp;
    uint32_t ret = 0;
    uint8_t tmpbuf[1024] = {0};

    //从分区读取到文件
    if(dd_flag == 0)
    {
        dd_flag = 1;
        char tmpbuf[200] = {0};
        sprintf(tmpbuf, "dd if=%s of=%s bs=1k count=64 > /dev/null", plat_config.device_info, DEVINFO);
        //system("dd if=/dev/mmcblk2p3 of=/tmp/xag-dls/hdls_devinfo bs=1k count=64 > /dev/null");
        system(tmpbuf);
    }

    fp = fopen(DEVINFO, "r");
    if(fp == NULL)
    {
        LOG(ERROR) << "fopen file hdls_devinfo fail！！！";
        return -1;
    }

    ret = fread(tmpbuf, 1, sizeof(tmpbuf), fp);
    if(ret <= 0)
    {
        LOG(ERROR) << "fread file hdls_devinfo fail！！！";
        fclose(fp);
        return -1;
    }
    fclose(fp);

    memcpy(&area, tmpbuf+0x30, 1);

    if(area > 6)
    {
        LOG(ERROR) << "area code error！！！";
        return -1;
    }

    return area;
}

int set_env()
{
    char * user;
    int ret = 0xac;

    if ((user = getenv("CITY")) != NULL) {
        printf("CITY = %s\n", user);
    }

    if ((user = getenv("USER")) != NULL) {
        printf("USER = %s\n", user);
    }

    ret = setenv("UU", "ac-develop", 1);
    printf("ret = %d; UU = %s\n", ret, getenv("UU"));

    //unsetenv("USER");
    //printf("USER = %s\n", getenv("USER"));

    putenv("CITY=guangzhou-yuexiu");
    printf("CITY = %s\n", getenv("CITY"));

    system("export AC=gz");

    return 0;
}

